﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using ITAP.ShprossesBuilder.Modelling.Comparers;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Modelling.Components.Actions
{
    public class ActionAlignSelectedShprossesPositions : IComponentAction
    {
        private ModelControl modelControl;
        private ComponentAlign align;
        private bool moveConnectedComponents;
        private RectangleF activeModelBoundingBox;    

        public ActionAlignSelectedShprossesPositions(ModelControl modelControl, ComponentAlign align, bool moveConnectedComponents, RectangleF boundingBox)
        {
            this.modelControl = modelControl;
            this.align = align;
            this.moveConnectedComponents = moveConnectedComponents;
            this.activeModelBoundingBox = boundingBox;
            
        }

        #region IComponentAction Members

        public bool Action(IComponent component)
        {
            if (!(component is Shpross))
                return false;

            Shpross shpross = component as Shpross;
            List<Shpross> componentsToAlign = new List<Shpross>();
            foreach (IComponent selectedComponent in this.modelControl.ModelContainer.SelectedComponentsStack)
                if (selectedComponent != component && selectedComponent is Shpross)
                    componentsToAlign.Add(selectedComponent as Shpross);

            

            ModelPoint moveToPoint;
            ModelVector offsetVector;
            switch (align)
            {
                case ComponentAlign.Horizontal:
                    componentsToAlign.Sort(new ShprossSelectedEndPointXAxisComparer());
                    foreach (Shpross selectedComponent in componentsToAlign)
                    {
                        Shpross shprossToAlign = selectedComponent as Shpross;
                        moveToPoint = new ModelPoint(shprossToAlign.Position.X, shpross.Position.Y);
                        offsetVector = new ModelVector(shprossToAlign.Position, moveToPoint);
                        shprossToAlign.MoveWithOffset(offsetVector);

                        if (moveConnectedComponents)
                        {
                            if (shprossToAlign.StartConnectedComponent != null && shprossToAlign.StartConnectedComponent.GetType() == typeof(Connector))
                                shprossToAlign.StartConnectedComponent.Parent.MoveWithOffset(offsetVector);
                            if (shprossToAlign.EndConnectedComponent != null && shprossToAlign.EndConnectedComponent.GetType() == typeof(Connector))
                                shprossToAlign.EndConnectedComponent.Parent.MoveWithOffset(offsetVector);
                        }
                    }
                    return true;
                case ComponentAlign.Vertical:
                    componentsToAlign.Sort(new ShprossSelectedEndPointYAxisComparer());
                    foreach (Shpross selectedComponent in componentsToAlign)
                    {
                        Shpross shprossToAlign = selectedComponent as Shpross;
                        moveToPoint = new ModelPoint(shpross.Position.X, shprossToAlign.Position.Y);
                        offsetVector = new ModelVector(shprossToAlign.Position, moveToPoint);
                        shprossToAlign.MoveWithOffset(offsetVector);

                        if (moveConnectedComponents)
                        {
                            if (shprossToAlign.StartConnectedComponent != null && shprossToAlign.StartConnectedComponent.GetType() == typeof(Connector))
                                shprossToAlign.StartConnectedComponent.Parent.MoveWithOffset(offsetVector);
                            if (shprossToAlign.EndConnectedComponent != null && shprossToAlign.EndConnectedComponent.GetType() == typeof(Connector))
                                shprossToAlign.EndConnectedComponent.Parent.MoveWithOffset(offsetVector);
                        }
                    }
                    return true;
                default:
                    throw new NotImplementedException();                    
            }
            return false;
        }

        #endregion
    }
}
